﻿import com.dmotiko.mirador.*
import com.dmotiko.general.*
import mx.transitions.Tween; 
import mx.transitions.easing.*;
import mx.utils.Delegate;

class MiradorMainBtnBar
extends MiradorBtnBar {
	private var mcQuienesSomos:MiradorBtn;
	private var mcNuestrosSalones:MiradorBtn;
	private var mcServicios:MiradorBtnGroup;
	private var mcEventosEmpresariales:MiradorBtn;
	private var mcOrganizacionDeEventos:MiradorBtn;
	private var mcContacto:MiradorBtnGroup;
	private var mcContactoBtnBar:MiradorContactBtnBar;
	private var mcServicesBtnBar:MiradorServicesBtnBar;
	
	private var mcTimer:MovieClip;
	private var nDelay:Number;
					
	private function initClip() {
		aBtns = [ mcQuienesSomos, mcNuestrosSalones, mcServicios, mcEventosEmpresariales, mcOrganizacionDeEventos, mcContacto ];
		aData = [ MiradorSite.QUIENES_SOMOS, MiradorSite.NUESTROS_SALONES, mcServicesBtnBar, MiradorSite.EVENTOS, MiradorSite.ORGANIZACION, mcContactoBtnBar ];  
						
		for( var i:Number = 0; i < aBtns.length; i++){
			aBtns[i]._alpha = 0;
			aBtns[i].initY = aBtns[i]._y;
			aBtns[i]._y += aBtns[i]._height;
		}
		
		mcTimer= this.createEmptyMovieClip("mcTimer", this.getNextHighestDepth());
		nDelay= 2;
		var nCount = 0;
		var nBoton = 0;
		mcTimer.onEnterFrame = function(){
			aBtns = _parent.aBtns;
			if(nCount == _parent.nDelay){
				nCount = 0;
				aBtns[nBoton].fade = new Tween(aBtns[nBoton], "_alpha", Regular.easeOut, aBtns[nBoton]._alpha, 100, 0.5, true);
				aBtns[nBoton].moved = new Tween(aBtns[nBoton], "_y", Regular.easeOut, aBtns[nBoton]._y,  aBtns[nBoton].initY, 0.3, true);
				nBoton++;
				if(nBoton == aBtns.length) delete this.onEnterFrame;
				
			} else {
				nCount++;
			}
		}
		
		MiradorSite.getApp().addEventListener( WebSite.SECTION_CHANGED, Delegate.create(this, section_changed));
		MiradorSite.getApp().setButtonBar(this);
	}
	
	private function section_changed( e ):Void {
		var sSection:String = MiradorSite.getApp().getSection();
		if ( sSection == MiradorSite.HOME ) this.reset();
		if (  sSection != MiradorSite.COMUNIQUESE && sSection != MiradorSite.UBICACION ) {
			mcContactoBtnBar.reset();
		}
		if (  sSection != MiradorSite.SERVICIOS_BARMITZVA 
			  && sSection != MiradorSite.SERVICIOS_QUINCE
			  && sSection != MiradorSite.SERVICIOS_BODAS
			) {
			mcServicesBtnBar.reset();
		}
	}
			
	private function btnSelected( evnt ):Void	{
		if ( evnt.description == "rollOver" ) {
			if ( evnt.target != mcContacto ) {
				mcContactoBtnBar.hide();
				mcContactoBtnBar.enabled = false;
			}
			if ( evnt.target != mcServicios ) {
				mcServicesBtnBar.hide();
				mcServicesBtnBar.enabled = false;
			}
			
		} else if (evnt.description == "setActive") {
			super.btnSelected(evnt);
		}
	}
	
	public function childLoaded(evnt):Void {
		if ( evnt.target == mcServicesBtnBar ) {
			evnt.target.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, services_changed));
			
		} else if ( evnt.target == mcContactoBtnBar) {
			evnt.target.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, contact_changed));
			 
		} else {
			bindBtn(evnt.target);
		}
	}
	
	private function services_changed( evnt ):Void {
		if ( evnt.data == MiradorSite.EVENTOS ) {
			mcServicesBtnBar.reset();
			activeButton( MiradorSite.EVENTOS );
			return;
		}
		if ( activeBtn != mcServicios ){
			mcServicios.setActive( true, true );
			activeBtn.setActive( false );
			activeBtn = mcServicios;
		}
		dispatchEvent( { type: BaseClipEvent.CHANGED, target: this, data: evnt.data } );
	}
	private function contact_changed( evnt ):Void {
		if ( activeBtn != mcContacto ){
			mcContacto.setActive( true, true );
			activeBtn.setActive( false );
			activeBtn = mcContacto;
		}
		dispatchEvent( { type: BaseClipEvent.CHANGED, target: this, data: evnt.data } );
	}
	
	public function activeButton( value ):Void {
		var btn = getButton( value );
		if ( !btn ) btn = mcContactoBtnBar.getButton( value );
		if ( !btn ) btn = mcServicesBtnBar.getButton( value );
		btn.onRollOver();
		btn.onRelease();
	}
	
}